﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

using KnifeZ.PersonalNotePad.Models;
using KnifeZ.XamarinDomain.Models;
using KnifeZ.PersonalNotePad.ViewModels;
using KnifeZ.XamarinDomain;

namespace KnifeZ.PersonalNotePad.Views
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class NewItemPage : ContentPage
    {
        public TodoItem Item { get; set; }

        public NewItemPage(TodoItem viewModel)
        {
            InitializeComponent();
            Item = viewModel;
            pickTaskType.Items.Add("日常琐事");
            pickTaskType.Items.Add("重要任务");
            pickTaskType.Items.Add("待办清单");
            pickTaskType.Items.Add("其他");

            BindingContext = this;
        }
        public NewItemPage()
        {
            InitializeComponent();
            Item = new TodoItem
            {
                Title = "",
                TaskType = "日常",
                EndTime = DateTime.Now.AddDays(1),
            };
            pickTaskType.Items.Add("日常琐事");
            pickTaskType.Items.Add("重要任务");
            pickTaskType.Items.Add("待办清单");
            pickTaskType.Items.Add("其他");
            BindingContext = this;
        }

        async void Save_Clicked(object sender, EventArgs e)
        {
            var isHasModel = await DataContext.Instance.FindAsync<TodoItem>(Item.ID);
            if (isHasModel != null)
            {
                await DataContext.Instance.UpdateAsync(Item);
            }
            else
            {
                await DataContext.Instance.InsertAsync(Item);
            }
            MessagingCenter.Send(this, "RefreshTodoList");
            await Navigation.PopModalAsync();
        }

        async void Cancel_Clicked(object sender, EventArgs e)
        {
            await Navigation.PopModalAsync();
        }
    }
}